package com.pcbly.web.persistence.dict;

import java.util.List;

import com.pcbly.web.bean.dict.DictCategoryCombBean;
import com.pcbly.web.bean.dict.DictCategoryValueBean;
import com.pcbly.web.persistence.HibernateBaseDao;

@SuppressWarnings("unchecked")
public class DictCategoryCombHibernateDao extends
		HibernateBaseDao<DictCategoryCombBean> implements DictCategoryCombDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4738280448213755958L;

	protected Class<DictCategoryCombBean> getEntityClass() {
		return DictCategoryCombBean.class;
	}

	public DictCategoryCombHibernateDao() {
		super();
	}

	@Override
	public void removeDictCategoryCombByDictId(String dictId) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("delete c from ");
		sb.append(" sys_dict_comb c ");
		sb.append(" where ");
		sb.append(" c.DICTID in ('");
		sb.append(dictId.replaceAll(",", "','"));
		sb.append("') ");
		getSession().createSQLQuery(sb.toString()).executeUpdate();
	}

	@Override
	public void removeDictCategoryCombByValueId(String valueId)
			throws Exception {
		// TODO Auto-generated method stub
		getSession().createQuery(
				"delete from DictCategoryCombBean where dictValueId=? ")
				.setString(0, valueId).executeUpdate();
	}

	@Override
	public List<DictCategoryValueBean> queryDictCategoryValueBeanById(String id)
			throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("select a from DictCategoryValueBean a ");

		sb.append(" ,  DictCategoryCombBean b  where  ");
		sb.append(" a.id=b.dictValueId ");
		sb.append("  and  b.dictId=? ");

		return getSession().createQuery(sb.toString()).setString(0, id).list();

	}

	@Override
	public DictCategoryValueBean findDictCategoryValueBeanById(String id)
			throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("from DictCategoryValueBean where id=? ");

		return (DictCategoryValueBean) getSession().createQuery(sb.toString())
				.setString(0, id).uniqueResult();
	}

}
